package com.hu.bgp.client.presenter;

import java.util.HashMap;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.hu.bgp.client.ClientGameContext;
import com.hu.bgp.client.EventBus;
import com.hu.bgp.client.widgets.uicomponents.GameSessionTable;
import com.hu.bgp.shared.data.ClientGameSession;
import com.hu.bgp.shared.data.ClientGameSessionCollection;
import com.hu.bgp.client.localevent.RequestPlaceChange;
import com.hu.bgp.client.place.*;
import com.hu.bgp.client.rpc.BgpServiceAsync;
import com.hu.bgp.framework.clientapi.GameID;
import com.hu.bgp.framework.clientapi.util.Firebug;

public class GameSessions implements PlacePresenter<GameSessionPlace>
{
	public interface Display
	{
		HasClickHandlers getJoinButton();

		HasClickHandlers getCreateButton();

		GameSessionTable getGameTypetable();

		Widget asWidget();
	}

	private GameSessionPlace place;
	private Display view;
	private BgpServiceAsync bgpService;
	private EventBus eventBus;

	@Inject
	public GameSessions(Display view, BgpServiceAsync bgpService, EventBus eventBus)
	{
		this.view = view;
		this.bgpService = bgpService;
		this.eventBus = eventBus;
		bind();
	}

	private void init()
	{
		bgpService.getClientGameSessionCollection(place.getGameAlias(), new AsyncCallback<ClientGameSessionCollection>()
		{

			public void onFailure(Throwable caught)
			{
				// TODO Auto-generated method stub

			}

			public void onSuccess(ClientGameSessionCollection result)
			{
				for (ClientGameSession cg : result)
					view.getGameTypetable().addGameRow(cg);

			}

		});

	}

	private void bind()
	{
		view.getJoinButton().addClickHandler(new ClickHandler()
		{

			public void onClick(ClickEvent event)
			{
				BoardgamePlace p = new BoardgamePlace();
				GameID gid = new GameID(place.getGameAlias());
				gid.setGameID(view.getGameTypetable().getSelection().getGameID());
				p.setGameID(gid);
				eventBus.fireEvent(new RequestPlaceChange(p));
			}

		});
		
		view.getCreateButton().addClickHandler(new ClickHandler()
		{

			public void onClick(ClickEvent event)
			{
				bgpService.createGame(place.getGameAlias(), new AsyncCallback<ClientGameSession>(){

					public void onFailure(Throwable caught)
					{
						// TODO Auto-generated method stub
						
					}

					public void onSuccess(ClientGameSession result)
					{
						BoardgamePlace p = new BoardgamePlace();
						GameID gid = new GameID(place.getGameAlias());
						gid.setGameID(result.getGameID());
						p.setGameID(gid);
						eventBus.fireEvent(new RequestPlaceChange(p));
						
					}
					
				});

			}

		});
	}

	public void go(final HasWidgets container)
	{
		container.clear();
		init();
		container.add(view.asWidget());
	}

	public void setPlace(GameSessionPlace place)
	{
		this.place = place;

	}

}
